﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net.MVC;
using Buffalo.Lib.Common;
using Buffalo.Tourism.DAL;
using System.Configuration;
using Buffalo.Tourism.DAL.Models;

namespace Buffalo.Web.Areas.Tourism.Controllers
{
    public class ReportController : Controller
    {
        public ActionResult GuestReport()
        {
            return this.View();
        }

        public ActionResult StatisticReport()
        {
            return this.View();
        }
        public ActionResult DetailReport()
        {
            return this.View();
        }
        public ActionResult FeedbackReport()
        {
            return this.View();
        }
        public ActionResult ReportView()
        {
            return this.View();
        }
        //[AcceptVerbs(HttpVerbs.Post)]
        //[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
        //    Justification = "Needs to take same parameter type as Controller.Redirect()")]
        public AjaxFormResult GenerateDetailReport(string datFromDate, string datToDate, string cboTourCode, string cboArea, string cboVehicle)
        {
            AjaxFormResult response = new AjaxFormResult();
            try
            {
                DateTime fromDate = Util.GetDateFromDMY(datFromDate);
                DateTime toDate = Util.GetDateFromDMY(datToDate);
                Tour tour = new TourProgramDAL().GetTourFromTourCode(cboTourCode );
                string area = cboArea.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboArea;
                string IDVehicle = cboVehicle;
                string locationRoleName = GeneralVar.gLoggedRoleName;

                string excelTemplateFile = CommonManager.GetModuleFolder() + @"\Template\TemplateDetailReport.xls";
                string connectionString = ConfigurationManager.ConnectionStrings[GeneralConstant.CONNECTION_STRING].ConnectionString;
                ReportDAL reportDAL= new  ReportDAL ();

                string reportFile = reportDAL.GenerateDetailReport(CommonManager.GetModuleFolder(),
                    excelTemplateFile,
                    GeneralConstant.CONNECTION_STRING,
                    fromDate, toDate,
                    tour != null ? tour.ID : string.Empty, area, IDVehicle, string.Empty, locationRoleName );

                response.ExtraParams["ReportFile"] = reportFile;
                return response;
            }
            catch
            {
                throw;
            }
        }
        public AjaxFormResult GenerateStatisticReport(string datFromDate, string datToDate, string cboTourCode, string cboArea)
        {
            AjaxFormResult response = new AjaxFormResult();
            try
            {
                DateTime fromDate = Util.GetDateFromDMY(datFromDate);
                DateTime toDate = Util.GetDateFromDMY(datToDate);
                Tour tour = new TourProgramDAL().GetTourFromTourCode(cboTourCode);
                string area = cboArea.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboArea;

                string excelTemplateFile = CommonManager.GetModuleFolder() + @"\Template\TemplateStatisticReport.xls";
                string connectionString = ConfigurationManager.ConnectionStrings[GeneralConstant.CONNECTION_STRING].ConnectionString;
                ReportDAL reportDAL = new ReportDAL();

                string reportFile = reportDAL.GenerateStatisticReport(CommonManager.GetModuleFolder(),
                    excelTemplateFile,
                    GeneralConstant.CONNECTION_STRING,
                    fromDate, toDate, tour != null ? tour.ID : string.Empty, area);

                response.ExtraParams["ReportFile"] = reportFile;
                return response;
            }
            catch
            {
                throw;
            }
        }
        public AjaxFormResult GenerateFeedbackReport(string datFromDate, string datToDate, string cboTourCode, string cboTourGuide)
        {
            AjaxFormResult response = new AjaxFormResult();
            try
            {
                DateTime fromDate = Util.GetDateFromDMY(datFromDate);
                DateTime toDate = Util.GetDateFromDMY(datToDate);
                Tour tour = new TourProgramDAL().GetTourFromTourCode(cboTourCode);
                TourGuide tourGuide = new TourGuideDAL().GetTourGuideByName(cboTourGuide);
                string IDTourGuide = tourGuide != null ? tourGuide.ID : string.Empty;

                string excelTemplateFile = CommonManager.GetModuleFolder() + @"\Template\TemplateFeedbackReport.xls";
                string connectionString = ConfigurationManager.ConnectionStrings[GeneralConstant.CONNECTION_STRING].ConnectionString;
                ReportDAL reportDAL = new ReportDAL();

                string reportFile = reportDAL.GenerateFeedbackReport(CommonManager.GetModuleFolder(),
                    excelTemplateFile,
                    GeneralConstant.CONNECTION_STRING,
                    fromDate, toDate, tour != null ? tour.ID : string.Empty, IDTourGuide);

                response.ExtraParams["ReportFile"] = reportFile;
                return response;
            }
            catch
            {
                throw;
            }
        }
        public AjaxFormResult GenerateGuestReport(string datFromDate, string datToDate, string cboTourCode, string cboArea)
        {
            AjaxFormResult response = new AjaxFormResult();
            try
            {
                DateTime fromDate = Util.GetDateFromDMY(datFromDate);
                DateTime toDate = Util.GetDateFromDMY(datToDate);
                Tour tour = new TourProgramDAL().GetTourFromTourCode(cboTourCode);
                string area = cboArea.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboArea;

                string excelTemplateFile = CommonManager.GetModuleFolder() + @"\Template\TemplateGuestReport.xls";
                string connectionString = ConfigurationManager.ConnectionStrings[GeneralConstant.CONNECTION_STRING].ConnectionString;
                ReportDAL reportDAL = new ReportDAL();

                string reportFile = reportDAL.GenerateGuestReport(CommonManager.GetModuleFolder(),
                    excelTemplateFile,
                    GeneralConstant.CONNECTION_STRING,
                    fromDate, toDate, tour != null ? tour.ID : string.Empty, area);

                response.ExtraParams["ReportFile"] = reportFile;
                return response;
            }
            catch
            {
                throw;
            }
        }
    }
}
